﻿using System;

using LastLand.Data.Global;

using Up2Link.SegString;
using Up2Link.Server;

namespace LastLand.Core.MsgProcessor
{
    public class CMsgDefend : IMessageProcessor
    {
        public void ProcessMessage(CMessagePack<CMessageSS> _msgPack)
        {
            Int32 index = _msgPack.index;
            if (CData.connections[index].IsInGame())
            {
                String account = CData.connections[index].Account;
                Int32 charaIndex = CData.userAccounts[account].ActiveCharacterIndex;
                if (CData.world.IsCharacterDead(charaIndex))
                {
                    CMessageSS characterDeadMsg = new CMessageSS(CConst.SERVER_MESSAGE_CHARACTER_DEAD, 0, CMessageSS.ENCODING_UTF32, "");
                    CMessagePack<CMessageSS> characterDeadMsgPack = new CMessagePack<CMessageSS>(characterDeadMsg, _msgPack.index);
                    CData.server.Send(characterDeadMsgPack);
                }
                else if (CData.world.IsCharacterInCombat(charaIndex))
                {
                    CMessageSS characterInCombatMsg = new CMessageSS(CConst.SERVER_MESSAGE_CHARACTER_IN_COMBAT, 0, CMessageSS.ENCODING_UTF32, "");
                    CMessagePack<CMessageSS> characterInCombatMsgPack = new CMessagePack<CMessageSS>(characterInCombatMsg, _msgPack.index);
                    CData.server.Send(characterInCombatMsgPack);
                }
                else if (!CData.world.IsGridInCombat(charaIndex))
                {
                    CMessageSS gridNotInCombatMsg = new CMessageSS(CConst.SERVER_MESSAGE_GRID_NOT_IN_COMBAT, 0, CMessageSS.ENCODING_UTF32, "");
                    CMessagePack<CMessageSS> gridNotInCombatMsgPack = new CMessagePack<CMessageSS>(gridNotInCombatMsg, _msgPack.index);
                    CData.server.Send(gridNotInCombatMsgPack);
                }
                else if (!CData.world.IsGridTerritory(charaIndex))
                {
                    CMessageSS gridNotTerritoryMsg = new CMessageSS(CConst.SERVER_MESSAGE_GRID_NOT_TERRITORY, 0, CMessageSS.ENCODING_UTF32, "");
                    CMessagePack<CMessageSS> gridNotTerritoryMsgPack = new CMessagePack<CMessageSS>(gridNotTerritoryMsg, _msgPack.index);
                    CData.server.Send(gridNotTerritoryMsgPack);
                }
                else if (!CData.world.GridHasFacilityOrCity(charaIndex))
                {
                    CMessageSS gridHasNoFacilityOrCityMsg = new CMessageSS(CConst.SERVER_MESSAGE_GRID_NO_FACILITY, 0, CMessageSS.ENCODING_UTF32, "");
                    CMessagePack<CMessageSS> gridHasNoFacilityCityMsgPack = new CMessagePack<CMessageSS>(gridHasNoFacilityOrCityMsg, _msgPack.index);
                    CData.server.Send(gridHasNoFacilityCityMsgPack);
                }
                else
                {
                    CData.world.Defend(charaIndex);
                    CMessageSS okDefendMsg = new CMessageSS(CConst.SERVER_MESSAGE_COMPLETE_DEFEND, 0, CMessageSS.ENCODING_UTF32, "");
                    CMessagePack<CMessageSS> okDefendMsgPack = new CMessagePack<CMessageSS>(okDefendMsg, _msgPack.index);
                    CData.server.Send(okDefendMsgPack);
                }
            }
            else if (CData.connections[index].IsLoggedIn())
            {
                CMessageSS clientNotPlayingMsg = new CMessageSS(CConst.SERVER_MESSAGE_CLIENT_NOT_PLAYING, 0, CMessageSS.ENCODING_UTF32, "");
                CMessagePack<CMessageSS> clientNotPlayingMsgPack = new CMessagePack<CMessageSS>(clientNotPlayingMsg, _msgPack.index);
                CData.server.Send(clientNotPlayingMsgPack);
            }
            else
            {
                CMessageSS clientNotLoggedInMsg = new CMessageSS(CConst.SERVER_MESSAGE_CLIENT_NOT_LOGGED_IN, 0, CMessageSS.ENCODING_UTF32, "");
                CMessagePack<CMessageSS> clientNotLoggedInMsgPack = new CMessagePack<CMessageSS>(clientNotLoggedInMsg, _msgPack.index);
                CData.server.Send(clientNotLoggedInMsgPack);
            }
        }
    }
}
